/**
 * 
 */
package info.niwota.web.apache;

import info.niwota.web.Debug;

import java.io.IOException;

import android.util.Log;

import x.org.apache.http.ConnectionReuseStrategy;
import x.org.apache.http.HttpException;
import x.org.apache.http.HttpRequest;
import x.org.apache.http.HttpResponse;
import x.org.apache.http.HttpResponseFactory;
import x.org.apache.http.HttpServerConnection;
import x.org.apache.http.HttpStatus;
import x.org.apache.http.params.HttpParams;
import x.org.apache.http.protocol.HttpContext;
import x.org.apache.http.protocol.HttpExpectationVerifier;
import x.org.apache.http.protocol.HttpProcessor;
import x.org.apache.http.protocol.HttpRequestHandler;
import x.org.apache.http.protocol.HttpRequestHandlerResolver;
import x.org.apache.http.protocol.HttpService;

/**
 * @author qiangli
 *
 */
public class WebHttpService extends HttpService {

	private static final String TAG = "WebHttpService";

	/**
	 * @param processor
	 * @param connStrategy
	 * @param responseFactory
	 * @param handlerResolver
	 * @param expectationVerifier
	 * @param params
	 */
	public WebHttpService(HttpProcessor processor,
			ConnectionReuseStrategy connStrategy,
			HttpResponseFactory responseFactory,
			HttpRequestHandlerResolver handlerResolver,
			HttpExpectationVerifier expectationVerifier, HttpParams params) {
		super(processor, connStrategy, responseFactory, handlerResolver,
				expectationVerifier, params);
	}

	/**
	 * @param processor
	 * @param connStrategy
	 * @param responseFactory
	 * @param handlerResolver
	 * @param params
	 */
	public WebHttpService(HttpProcessor processor,
			ConnectionReuseStrategy connStrategy,
			HttpResponseFactory responseFactory,
			HttpRequestHandlerResolver handlerResolver, HttpParams params) {
		super(processor, connStrategy, responseFactory, handlerResolver, params);
	}

	@Override
	public void handleRequest(HttpServerConnection conn, HttpContext context)
			throws IOException, HttpException {
		if (Debug.DEBUG) {
			Log.d(TAG, "handleRequest " + conn);
		}
		super.handleRequest(conn, context);
	}

	@Override
	protected void handleException(HttpException ex, HttpResponse response) {
		if (Debug.DEBUG) {
			ex.printStackTrace();
			Log.d(TAG, "handleException " + ex);
		}
		super.handleException(ex, response);
	}

	@Override
	protected void doService(HttpRequest request, HttpResponse response,
			HttpContext context) throws HttpException, IOException {
		if (Debug.DEBUG) {
			Log.d(TAG, "doService " + request.getRequestLine());
		}
		//super.doService(request, response, context);
        HttpRequestHandler handler = null;
        
        if (this.handlerResolver != null) {
            //String requestURI = request.getRequestLine().getUri();
        	WebRequestHandlerResolver resolver = (WebRequestHandlerResolver) this.handlerResolver;
            handler = resolver.lookup(request);
        }
        if (handler != null) {
            handler.handle(request, response, context);
        } else {
            response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
        }
	}
}
